package com.itheima.prince.smartpeking.activity;

import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.itheima.prince.smartpeking.R;
import com.itheima.prince.smartpeking.utils.SPUtil;

import java.util.ArrayList;
import java.util.List;

public class GuideUi extends AppCompatActivity implements View.OnClickListener {

    private ViewPager mvp_guide_bg;
    private List<ImageView> imageViews;
    private LinearLayout ll_guide_group;
    private ImageView miv_reddot;
    private Button mbt_guide_start;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
        setContentView(R.layout.activity_guide_ui);
        mvp_guide_bg = (ViewPager) findViewById(R.id.vp_guide_bg);
        //找到横向的线性布局，动态的添加孩子
        ll_guide_group = (LinearLayout) findViewById(R.id.ll_guide_group);
        miv_reddot = (ImageView) findViewById(R.id.iv_reddot);
        mbt_guide_start = (Button) findViewById(R.id.bt_guide_start);
        mbt_guide_start.setOnClickListener(this);


        initdata();
        mvp_guide_bg.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return imageViews.size();
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                //固定写法
                container.removeView((View) object);
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                //不管返回任何VIEW，都要先添加到容器中
                ImageView imageView = imageViews.get(position);
                container.addView(imageView);
                return imageView;
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }
            //固定写法
        });

        mvp_guide_bg.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //手指移动的距离/屏幕的宽度=红点移动的距离/两个灰点距离==positionoffset
                int redPosition = (int) ((position+positionOffset)*40);
                Log.d("aaa","position:" + position + "positionOffset:" + positionOffset);
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) miv_reddot.getLayoutParams();
                layoutParams.leftMargin = redPosition;
                miv_reddot.setLayoutParams(layoutParams);

            }

            @Override
            public void onPageSelected(int position) {
                //当滑动到某一页时执行
                if (position==(imageViews.size()-1)){
                    mbt_guide_start.setVisibility(View.VISIBLE);
                }else {
                    mbt_guide_start.setVisibility(View.GONE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initdata() {
        int[] imgIds = new int[]{R.mipmap.guide_1, R.mipmap.guide_2, R.mipmap.guide_3};
        imageViews = new ArrayList<>();
        for (int i = 0; i < imgIds.length; i++) {
            ImageView imageView = new ImageView(this);
            imageView.setImageResource(imgIds[i]);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageViews.add(imageView);

            ImageView point = new ImageView(this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(20, 20);
            if (i != 0) {
                params.leftMargin = 20;
            }
            point.setLayoutParams(params);//空间要导入到哪一个布局中，就导哪一个的布局
            point.setBackgroundResource(R.drawable.guide_point_normal);
            ll_guide_group.addView(point);
        }
    }

    @Override
    public void onClick(View view) {
        startActivity(new Intent(GuideUi.this,MainActivity.class));
        SPUtil.putBoolean(this,WelcomeUI.IS_APP_FIRST_OPEN,false);
        finish();
    }
}
